/**
 * 房間中籌碼視圖控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-3-8 15:57
 */
package com.addcn.texas.view
{
	import com.addcn.texas.utils.Pot;
	import flash.display.*;
	import flash.events.Event;	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;		
	import com.addcn.texas.model.*;
	import com.addcn.texas.control.*;
	import com.addcn.texas.view.components.*;	
	import com.addcn.texas.utils.Coordinate;
	import com.addcn.texas.utils.Chips;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
	import com.addcn.common.event.ParamEvent;
	
	import caurina.transitions.Tweener;
	
	public class  ChipsView  extends View 
	{		
		public static const MOVE_CHIPS_COMPLETE : String = "moveChipsComplete";
		
		private var _container : Object;
		private var _app : AppLibary;
		private var _components : Object;		
		private var _bet_class : Class;
		private var _chip_pos : Array;
		private var _pool_pos : Array;
		private var _sit_pos : Array;
		
		private var _chip_tmp : Object;  //玩家下注的籌碼對象		
		private var _chip_arr : Array;   //筹码对象数组
		private var _pool_arr : Array;   //保存池底對象
		
		private var _pot_data : Array;  //保存池底对象		
		private var _result : Object;
		private var _lose_result : Object;
		
		

		
		public function ChipsView(r_target : Object) : void
		{
			this._container = r_target;
			_initialize();
		}
		
		private function _initialize() : void
		{
			//构建容器
			var _sprite = new Sprite;
			this._container.addChild(_sprite);
			this._container = _sprite;				
			
			this._app = AppLibary.getInstance();	
			this._bet_class = _app.getClass("com.addcn.texas.view.components.BetPanel");
			this._components = {};	
			this._chip_pos = Coordinate.getInstance().ChipCoord();
			this._pool_pos = Coordinate.getInstance().PoolCoord();
			this._sit_pos = Coordinate.getInstance().SitCoord();
			this._chip_tmp = {};
			this._chip_arr = [];
			this._pool_arr = [];			
			this._pot_data = [];			
		}
		
		override public function notificationsList():void 
		{
			notifications = [							
							GameInfoModel.GAME_START,														
							GameInfoModel.USER_BET,						
							GameInfoModel.POOL_LIST,
							GameInfoModel.GAME_OVER,
							GameInfoModel.LOSE_PLAYER,
							PokerView.ALLOT_CHIPS,
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {			
				case GameInfoModel.GAME_START:
					this._result = null;
					//this._clearChip();
					//this._result = null;
					break;										
				case GameInfoModel.USER_BET:   //下注						
					_showBetChip(r_data);
					break;
				case GameInfoModel.POOL_LIST:  //划分奖池					
					this._pot_data = r_data as Array;
					MonsterDebugger.trace("pool_list", r_data)
					_amassChips();
					break;
				case GameInfoModel.GAME_OVER:  //游戲結束,分配池底					
					this._result  = r_data;		
					_amassChips();
					break;
				case GameInfoModel.LOSE_PLAYER:
					this._lose_result = r_data;
					_amassChips();
					MonsterDebugger.trace("lose user" , r_data)
					//_losePlayer();
					break;                       
				case PokerView.ALLOT_CHIPS:
					//this._result = r_data;
					//_allotChips();
					break;
			
			}
		}         
			
		/**
		 * 顯示玩家下注的籌碼
		 * @param	r_data Object
		 */
		private function _showBetChip(r_data : Object) : void
		{					
			var _sit : int = r_data["sit"];
			var _chip_obj : Chips = this._chip_tmp[_sit];
			var _chip : int = r_data["bet"];
			if (_chip > 0) {
				if (!_chip_obj) {
					_chip_obj = new Chips(_chip);
					_chip_obj.setPosition(_chip_pos[_sit]["x"], _chip_pos[_sit]["y"]);
					_chip_obj.show(this._container);
					this._chip_tmp[_sit] = _chip_obj;
				}else {
					_chip += _chip_obj.amount;
					_chip_obj.modifyChip(_chip);
				}
				
			}								
		}
				
		
						
		/**
		 * 將玩家下注的籌碼集中
		 * @param	r_data Array [int,int,..] 池底
		 */
		private function _amassChips() : void
		{								
			var _chip : Chips;
			var _option : Object = {};				
			var _pool_len : int = this._pool_arr.length - 1;		
			var _pool_x : int = this._pool_pos[4]["x"];
			var _pool_y : int = this._pool_pos[4]["y"];		
			for (var k in _chip_tmp ) {
				_chip = this._chip_tmp[k] as Chips;					
				if (_chip) {					
					_option["x"] = _pool_x; 
					_option["y"] = _pool_y;	
					_option["time"] = 0.6;		
					_option["delay"] = 1;
					_option["onComplete"] = _tweenComplete;
					_option["transition"] = "easeOutQuart";	
					_option["onCompleteParams"] = [_chip];				
					Tweener.addTween(_chip, _option);	
					_chip.removeText();
				}
				delete this._chip_tmp[k] ;
			}		
			
		}		
				
		private function _tweenComplete(r_chip : Chips ) : void
		{				
			Tweener.removeTweens(r_chip, "x", "y");
			r_chip.remove();  //移除籌碼				
			_dividePot();
			
		}
		
		/**
		 * 劃分獎池
		 */
		private function _dividePot() : void
		{			
			if (!this._pot_data) return;						
			var _data : Array = this._pot_data;
			this._pot_data = null;
			var _obj : Object;
			var _pot : int;
			var _chip : Chips;
			var _len : int = _data.length;
			var _index : int = Math.floor((9 - _len) * 0.5 );
			for (var k in _data) {	
				_obj = this._pool_arr[k];
				if (_obj) {
					_obj.parent.removeChild(_obj);
				}
				_obj = _data[k];
				_pot = _obj["pot"];				
				_chip = new Chips(_pot);
				_chip.setPosition(this._pool_pos[_index]["x"],this._pool_pos[_index]["y"]);
				_chip.show(this._container);
				this._pool_arr[k] = _chip;
				_index++;
			}
			if (this._result) {
				var _timer : Timer = new Timer(1000, 1);
				_timer.addEventListener(TimerEvent.TIMER, _distributeChip);
				_timer.start();
			}else if (this._lose_result) {
				_losePlayer();
			}
		}
		
		
		private function _distributeChip(evt : TimerEvent) : void
		{			
			evt.target.stop();
			evt.target.removeEventListener(TimerEvent.TIMER, _distributeChip);
			var _data : Object = this._result.pot;						
			this._result = null;
			var _obj : Chips;
			var _win_sit : Array;	
			var _view : PokerView = ViewLocator.getInstance().getView(PokerView);
			var _model : RoomUserModel = ModelLocator.getInstance().getModel(RoomUserModel);
			var _len : int;
			for (var k in _data) {				
				_obj = this._pool_arr[k];
				delete this._pool_arr[k];
				if (_obj) {
					_win_sit = _data[k]["sit"];
					_view.showMadeHand(_win_sit);	
					_len = _win_sit.length;
					if (_len > 1) {  //同一池底有多個贏家
						var _cut_chip : int = _data[k]["pot"] / _len;		
						var _user_chip : int;
						for (; _len > 1; _len-- ) {								
							_obj = _obj.cut(_cut_chip);
							_obj.move(this._sit_pos[_win_sit[_len - 1]]["x"], this._sit_pos[_win_sit[_len - 1]]["y"]);
							_user_chip = _model.getUserChip(_data[k]["uid"][_len - 1]);
							_model.modifyUserChip(_data[k]["uid"][_len - 1], _user_chip + _cut_chip);								
						}
					}			
					_user_chip = _model.getUserChip(_data[k]["uid"][0]);
					_model.modifyUserChip(_data[k]["uid"][0], _user_chip + _obj.amount);		
					_obj.move(this._sit_pos[_win_sit[0]]["x"], this._sit_pos[_win_sit[0]]["y"]);
				}
			}
		}
		
		private function _losePlayer() : void
		{
			var _data = this._lose_result;
			var _sit : int = _data["sit"];
			var _chip : int = _data["chip"];
			var _uid : int  =_data["uid"];
			var _obj : Object;
			for (var k in this._pool_arr) {			
				_obj = this._pool_arr[k];
				_obj.move(this._sit_pos[_sit]["x"], this._sit_pos[_sit]["y"]);				
			}
			var _model : RoomUserModel = ModelLocator.getInstance().getModel(RoomUserModel);
			var _user_chip : int = _model.getUserChip(_uid);
			_model.modifyUserChip(_uid, _user_chip + _chip);
			this._lose_result = null;
		}
				
		override public function destroy():void 
		{
			RemoveAllChildren.remove(this._container as DisplayObject);
		}
		
	}
	
}
